<?php
namespace Admin\Model;
use Think\Model;
class RollsModel extends Model {
    protected $tableName = 'rolls';//什么表名称
    public $errNotice = '';
    public $imagePath = '';

    protected $_validate = array(
       array('imageName','require','你还没有填写图片名称'),
        array('imageDesc','require','你还没有填写图片描述'),
        //array('imageUpload','require','你还没有填写图片描述1'),
        array('content','',''),
    );//字段验证

    //查询
    public function selectList()
    {
       return $rollObj =  $this->order('id')->select();
    }

    //轮播验证验证
    public function rollVerify()
    {
        if(!$this->create()){
           return $this->getError();
        }
    }

    //文件上传
    /*public function imageUpload(){
        $user_path='./Uploads/';
        $file_true_name=$_FILES['imageUpload']['name'];
        $move_to_file=$user_path."/".time().rand(1,1000).substr($file_true_name,strrpos($file_true_name,"."));
        if($_FILES['imageUpload']['error'] == 0){
          move_uploaded_file($_FILES['imageUpload']['tmp_name'],iconv("utf-8","gb2312",$move_to_file));
           return true;
        }
    }*/
    //错误提示
    public function err()
    {
        return $this->errNotice;
    }

    public function messg()
    {
        return $this->imagePath;
    }
    //文件上传
    public function imageUpload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     314572800000000 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
        // 上传单个文件
        $info   =   $upload->uploadOne($_FILES['imageUpload']);
        if(!$info) {// 上传错误提示错误信息
            $this->errNotice = $upload->getError();
            return $this->err();
        }
            $this->imagePath = $info;
            return $this->messg();
    }

    //轮播单独删除
    public function deletList($id)
    {
        if(!empty($id)){
            $link = $this->where(['id'=> $id])->find();
            $filename = './'.$link['url'];
            if($filename){
                if(file_exists($filename)){
                    $res = unlink($filename);
                   if($res){
                     return  $this->where(['id' =>$id])->delete();
                   }
                }
            }
        }
    }
    //轮播图片删除
    public function imageDel($id)
    {
        $link = $this->where(['id'=> $id])->find();
        $filename = './'.$link['url'];
        if($filename){
            if(file_exists($filename)){
               return  unlink($filename);
            }
        }

    }

    //轮播批量删除
    public function delAll($id)
    {
       if(!empty($id)){
           $map['id']  = array('in',$id);
           $link = $this->where($map)->select();
           $coun = count($link);
           for ($i=0; $i<$coun; $i++)
           {
              $filename = './'.$link[$i]['url'];
              if($filename){
                  if(file_exists($filename)){
                      $res = unlink($filename);
                  }
              }
           }
           return $this->where($map)->delete();
       }
    }

}